package com.sinodata.bsm.cicp.util.telnet;

import java.io.IOException;

/**
 * Telnet/ssh/ssh2 封装的接口.
 * 
 * @author thomas.h.zhang
 */
public interface Telnet {
    public static final int TIMEOUT_SECOND = 35; // 35's

    /** Connect the socket and open the connection. */
    public void connect(String host, int port) throws IOException;

    /** Disconnect the socket and close the connection. */
    public void disconnect() throws IOException;

    //设置其是否是连接正常的
    public void setIsValid(boolean isValid);

    //得到其连接状态
    public boolean getIsValid();

    /**
     * Set the prompt for the send() method.
     */
    public void setPrompt(String prompt);

    /**
     * Get the prompt.
     */
    public String getPrompt();

    /**
     * Login into remote host. This is a convenience method and only works if
     * the prompts are "login:" and "Password:".
     * 
     * @param user
     *            the user name
     * @param pwd
     *            the password
     */
    public void login(String user, String pwd, String prompt) throws IOException;

    public void write(byte[] b) throws IOException;

    public int read(byte[] b) throws IOException;

    /**
     * Send a command to the remote host. A newline is appended and if a prompt
     * is set it will return the resulting data until the prompt is encountered.
     * 
     * @param cmd
     *            the command
     * @return output of the command or null if no prompt is set
     */
    public void sendCmd(String cmd) throws IOException;

    /**
     * Read data from the socket and use telnet negotiation before returning the
     * data read.
     * 
     * @param b
     *            the input buffer to read in
     * @return the amount of bytes read
     */
    public String recvResp() throws IOException;

    /**
     * 发送取消指令^C
     * 
     * @throws IOException
     */
    public void cancel() throws IOException;

    /**
     * Execute shell command, and recv the response.
     * 
     * @param cmd
     * @return
     * @throws TelnetException
     */
    public String runTask(String shell) throws IOException;

    /**
     * Login on the telnet server task.
     * 
     * @param user
     * @param pwd
     * @param prompt
     * @throws TelnetException
     */
    public void runLogin(String user, String pwd, String prompt) throws IOException;

}
